package com.jm.product.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jm.product.controller.dto.TypePropsModel;
import com.jm.product.entity.Brand;
import com.jm.product.entity.GoodsType;
import com.jm.product.entity.Specification;
import com.jm.product.entity.TypeBrandMap;
import com.jm.product.entity.TypeSpecMap;
import com.jm.product.entity.dto.Attribute;
import com.jm.product.entity.dto.ParamGroup;
import com.jm.product.service.BrandService;
import com.jm.product.service.GoodsTypeService;
import com.jm.product.service.SpecificationService;

/**
 * 商品类型Controller
 * @author sunshine
 * @version 2017-06-30
 */
@Controller
@RequestMapping(value = "product/goodsType")
public class GoodsTypeController extends com.jm.product.controller.core.BaseController<GoodsType,Integer> {

	@Resource
	private GoodsTypeService goodsTypeService;
	@Resource
	private BrandService brandService;
	@Resource
	private SpecificationService specService;
	
	protected String listPage() {

		return "product/goodsTypeList";
	}

	public String form(GoodsType goodsType, Model model) {
		/*
		if (null != goodsType && null != goodsType.getId()){
			this.data = service.getById(goodsType.getId());
			model.addAttribute("goodsType", this.data);
		}
		*/
		model.addAttribute("goodsType", this.data);
		return "product/goodsTypeForm";
	}

	public String savePage() {

		return "redirect:/product/goodsType/?repage";
	}

	@RequestMapping("typeProps")
	public String typeProps(@RequestParam Integer id, Model model){
		String props = this.data.getProps() ;
		List<Attribute> attrs = JSON.parseArray(props, Attribute.class) ;
		model.addAttribute("attributes", attrs) ;
		model.addAttribute("type", data);
		return "product/typeProps" ;
	}
	
	@RequestMapping("typePropsJson")
	@ResponseBody
	public List<Attribute> typePropsJson(@RequestParam Integer id){
		String props = this.data.getProps() ;
		List<Attribute> attrs = JSON.parseArray(props, Attribute.class) ;
		return attrs;
	}
	
	@RequestMapping("saveTypeProps")
	public String saveTypeProps(Integer id, TypePropsModel typePropsModel, RedirectAttributes redirectAttributes){
		try {
			String props = JSON.toJSONString(typePropsModel.getAttributes(), SerializerFeature.WriteMapNullValue,
							SerializerFeature.WriteNullListAsEmpty,
			        		SerializerFeature.WriteNullStringAsEmpty);
			this.data.setProps(props);
			this.service.save(data);
			addMessage(redirectAttributes, "保存成功");
		} catch (Exception e) {
			addMessage(redirectAttributes, "保存失败");
			e.printStackTrace();
		}
		return "redirect:/product/goodsType/?repage";
	}

	@RequestMapping("typeParams")
	public String typeParams(@RequestParam Integer id, Model model){
		String params = this.data.getParams() ;
		List<ParamGroup> pgs = JSON.parseArray(params, ParamGroup.class) ;
		model.addAttribute("paramGroups", pgs);
		model.addAttribute("type", data);
		return "product/typeParams" ;
	}
	
	@RequestMapping("saveTypeParams")
	public String saveTypeParams(@RequestParam Integer id, @RequestParam String paramnum, 
			@RequestParam String[] groupnames, @RequestParam String[] paramnames, RedirectAttributes redirectAttributes){
		try {
			if(StringUtils.isNotBlank(paramnum)){
				String[] paramnums = new String[0];
				paramnums = paramnum.split(",");
				List<ParamGroup> list = goodsTypeService.toParamList(paramnums, groupnames, paramnames, null);
				String params = JSON.toJSONString(list, SerializerFeature.WriteMapNullValue,
						SerializerFeature.WriteNullListAsEmpty,
		        		SerializerFeature.WriteNullStringAsEmpty);
				this.data.setParams(params);
				this.service.save(data);
				addMessage(redirectAttributes, "保存成功");
			}
		} catch (Exception e) {
			addMessage(redirectAttributes, "保存失败");
			e.printStackTrace();
		}
		return "redirect:/product/goodsType/?repage";
	}
	
	@RequestMapping("typeBrand")
	public String typeBrand(GoodsType goodsType, Model model){
		List<Brand> brands = brandService.list(null, null, 0, Integer.MAX_VALUE) ;
		model.addAttribute("brands", brands);
		if(this.data != null) {
			List<TypeBrandMap> tbMap = this.data.getTypeBrandMap() ;
			model.addAttribute("selectedBrands", tbMap);
			model.addAttribute("type", data);
		}
		return "product/typeBrand" ;
	}
	
	@RequestMapping("saveTypeBrand")
	public String saveTypeBrand(@RequestParam Integer id, @RequestParam Integer[] chooseBrands, RedirectAttributes redirectAttributes) {
		try {
			List<TypeBrandMap> tbMap = this.data.getTypeBrandMap() ;
			tbMap.clear();
			for(Integer brandId : chooseBrands){
				Brand brand = new Brand();
				brand.setId(brandId);
				TypeBrandMap map = new TypeBrandMap() ;
				map.setBrand(brand);
				map.setGoodsType(data);
				tbMap.add(map);
			}
			this.service.save(data);
			addMessage(redirectAttributes, "保存成功");
		} catch (Exception e) {
			addMessage(redirectAttributes, "保存失败");
			e.printStackTrace();
		}
		return "redirect:/product/goodsType/?repage";
	}
	
	@RequestMapping("typeSpec")
	public String typeSpec(GoodsType goodsType, Model model){
		List<Specification> specs = specService.list(null, null, 0, Integer.MAX_VALUE);
		model.addAttribute("specs", specs);
		if(this.data != null) {
			List<TypeSpecMap> tsMap = this.data.getTypeSpecMap() ;
			model.addAttribute("selectedSpecs", tsMap);
			model.addAttribute("type", data);
		}
		return "product/typeSpec" ;
	}
	
	@RequestMapping("saveTypeSpec")
	public String saveTypeSpec(@RequestParam Integer id, @RequestParam Integer[] chooseSpecs, RedirectAttributes redirectAttributes) {
		try {
			List<TypeSpecMap> tsMap = this.data.getTypeSpecMap() ;
			tsMap.clear();
			for(Integer specId : chooseSpecs){
				Specification spec = new Specification();
				spec.setId(specId);
				TypeSpecMap map = new TypeSpecMap();
				map.setSpecification(spec);
				map.setGoodsType(data);
				tsMap.add(map);
			}
			this.service.save(data);
			addMessage(redirectAttributes, "保存成功");
		} catch (Exception e) {
			addMessage(redirectAttributes, "保存失败");
			e.printStackTrace();
		}
		return "redirect:/product/goodsType/?repage";
	}
}
